package com.gzc.just.play.last.war.common.share.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;

/**
 * 环境工具类
 * 提供环境相关的实用方法
 */
public class EnvironmentUtils {
    
    private static final Logger logger = LoggerFactory.getLogger(EnvironmentUtils.class);
    
    /**
     * 检查是否处于开发环境
     */
    public static boolean isDevelopment(Environment environment) {
        return environment.acceptsProfiles("dev-all-in-one");
    }
    
    /**
     * 检查是否处于生产环境
     */
    public static boolean isProduction(Environment environment) {
        return environment.acceptsProfiles("prod");
    }
    
    /**
     * 获取当前激活的Profile
     */
    public static String getActiveProfile(Environment environment) {
        String[] activeProfiles = environment.getActiveProfiles();
        if (activeProfiles.length > 0) {
            return activeProfiles[0];
        }
        return "default";
    }
    
    /**
     * 检查是否支持All-in-One模式
     */
    public static boolean supportsAllInOneMode(Environment environment) {
        return isDevelopment(environment) || 
               environment.getProperty("all-in-one.enabled", Boolean.class, false);
    }
    
    /**
     * 获取数据库类型
     */
    public static String getDatabaseType(Environment environment) {
        if (isDevelopment(environment)) {
            return "SQLite";
        } else if (isProduction(environment)) {
            return "MySQL";
        }
        return "Unknown";
    }
    
    /**
     * 获取缓存类型
     */
    public static String getCacheType(Environment environment) {
        if (isDevelopment(environment)) {
            return "Caffeine";
        } else if (isProduction(environment)) {
            return "Redis";
        }
        return "Unknown";
    }
    
    /**
     * 获取环境信息摘要
     */
    public static String getEnvironmentSummary(Environment environment) {
        return String.format("环境: %s, 数据库: %s, 缓存: %s, All-in-One: %s",
            getActiveProfile(environment),
            getDatabaseType(environment),
            getCacheType(environment),
            supportsAllInOneMode(environment));
    }
    
    /**
     * 验证环境配置
     */
    public static boolean validateEnvironment(Environment environment) {
        try {
            String activeProfile = getActiveProfile(environment);
            
            if ("dev-all-in-one".equals(activeProfile)) {
                // 检查开发环境必要配置
                String dbPath = environment.getProperty("sqlite.database.path");
                if (dbPath == null || dbPath.trim().isEmpty()) {
                    logger.warn("开发环境缺少SQLite数据库路径配置");
                    return false;
                }
            } else if ("prod".equals(activeProfile)) {
                // 检查生产环境必要配置
                String dbUrl = environment.getProperty("spring.datasource.url");
                String redisHost = environment.getProperty("spring.redis.host");
                
                if (dbUrl == null || dbUrl.trim().isEmpty()) {
                    logger.warn("生产环境缺少数据库URL配置");
                    return false;
                }
                
                if (redisHost == null || redisHost.trim().isEmpty()) {
                    logger.warn("生产环境缺少Redis主机配置");
                    return false;
                }
            }
            
            return true;
        } catch (Exception e) {
            logger.error("环境配置验证失败", e);
            return false;
        }
    }
}